package com.process.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.process.controller.util.Code;
import com.process.controller.util.R;
import com.process.dao.pojoDao.StudentDao;
import com.process.dao.team.*;
import com.process.dao.voDao.CampusInnerSubtaskVoDao;
import com.process.dao.voDao.CampusOuterSubtaskVoDao;
import com.process.domain.outer.CampusOuterSubtaskVo;
import com.process.domain.publi.Student;
import com.process.domain.publish.CampusInnerSubtaskVo;
import com.process.domain.team.*;
import com.process.service.team.TeamService;
import com.process.service.team.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @ClassName: TeamController
 * @Author: 殷凡卓
 * @Date: 2023/5/1 10:24
 */
@RestController
@Slf4j
@RequestMapping("/students/team")
public class TeamController {
    @Autowired
    private TeamService teamService;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private MemberVoDao memberVoDao;
    @Autowired
    private TopicDao topicDao;
    @Autowired
    private TeamApplyDao teamApplyDao;
    @Autowired
    private TopicVoDao topicVoDao;
    @Autowired
    private TeamDao teamDao;
    @Autowired
    private CampusInnerSubtaskVoDao campusInnerSubtaskVoDao;
    @Autowired
    private TopicService topicService;
    @Autowired
    private CampusOuterSubtaskVoDao campusOuterSubtaskVoDao;


    @GetMapping("create")
    public R getCreate(ServletRequest servletRequest) {
        log.info("组队controller");
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
        long stuId = student.getStuId();

//这个创建的service包含权限判断
        Integer team = teamService.createTeam(stuId);
        if (team == Code.SAVE_OK) {
//            创建成功
            return new R(Code.SAVE_OK, null, "创建成功");
        } else {
//            创建失败
            return new R(Code.SAVE_ERR, "您已是队伍成员，创建失败", "创建失败");
        }
    }

    @GetMapping("delete")
    public R getDelete(ServletRequest servletRequest) {

        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
        long stuId = student.getStuId();
        Integer deleteTeam = teamService.deleteTeam(stuId);

        if (deleteTeam == Code.DELETE_OK) {
//            删除成功
            return new R(Code.DELETE_OK, null, "删除成功");
        } else {
//            删除失败
            return new R(Code.DELETE_ERR, "您不是队长 || 队伍不存在", "删除失败");
        }
    }

    @GetMapping("applySend/{apply}")
    public R getApplySend(@PathVariable long apply, ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStuPhone, username));
        if (student != null) {
            long send = student.getStuId();
            R r = teamService.applySend(send, apply);
            return r;
        } else
            return new R(Code.SAVE_ERR, student, "您不是队长无权限邀请");


    }

    @GetMapping("myApplySend")
    public R getMyApplySend(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
//       我的学号
        long mySend = student.getStuId();
        List<TeamApplyVo> send = teamService.getSend(mySend);
//        List<TeamApply> send = teamService.getSend(mySend);
        return new R(Code.GET_OK, send, "我发送的通知");
    }

    @GetMapping("myApplyAccept")
    public R getMyApplyAccept(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
        long myAccept = student.getStuId();
        List<TeamApplyVo> accept = teamService.getaccept(myAccept);
//        List<TeamApply> accept = teamService.getaccept(myAccept);
        return new R(Code.GET_OK, accept, "我接受的通知");
    }

    @GetMapping("myMembers")
    public R getMyMembers(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
        long stuId = student.getStuId();
        List<MemberVo> myMembers = teamService.getMyMember(stuId);
        if (myMembers != null)
            return new R(Code.GET_OK, myMembers, "我的队员们");
        else
            return new R(Code.GET_ERR, myMembers, "没有组员");

    }

    @GetMapping("acceptToTeam/{teamId}/{applyId}")
    public R getAcceptToTeam(@PathVariable long teamId, @PathVariable long applyId, ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStuPhone, username));
        long stuId = student.getStuId();
        Integer accept = teamService.acceptToTeam(teamId, stuId);

//        更改信息已读状态
        LambdaUpdateWrapper<TeamApply> set = new LambdaUpdateWrapper<TeamApply>()
                .eq(TeamApply::getApplyId, applyId)
                .set(TeamApply::getApplyState, 1);
        int update = teamApplyDao.update(null, set);


        if (accept > 0)
            return new R(Code.GET_OK, null, "成功入队");
        else
            return new R(Code.GET_ERR, null, "您已是某队伍成员");

    }

    @GetMapping("innerTopic/{PublishId}")
    public R getinnerTopic(@PathVariable long PublishId, ServletRequest servletRequest) {

//      获取我的学号
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
        long stuId = student.getStuId();
//      获取_本学期_我的member,
        MemberVo myDetil = memberVoDao.getMyDetil(stuId);
//        log.info(myDetil.toString());

        if (myDetil == null) {
            return new R(Code.TOPIC_ERR, myDetil, "您不是队长无权限，请创建队伍，或联系队长选题");
        } else if (myDetil.getMemberState() == 1 && myDetil.getTeamState() == 1) {
            //       已经队长
            return new R(Code.TOPIC_ERR, myDetil, "您已选题，不要多次选题");
        } else if (myDetil.getMemberState() == 1 && myDetil.getTeamState() == 0) {
            //      选题成功  填充Topic
            Topic topic = new Topic();
            topic.setPublishId(PublishId);
            topic.setTopicTeamId(myDetil.getTeamId());
            topic.setTopicType(1);
            topic.setTopicDate(new Date());
            topicDao.insert(topic);
            //      更改队伍表选题states

            int update = teamDao.update(null, new LambdaUpdateWrapper<Team>()
                    .eq(Team::getTeamId, myDetil.getTeamId())
                    .set(Team::getTeamState, 1));

            return new R(Code.TOPIC_OK, topic, "队长选题成功");
        } else
            return new R(Code.TOPIC_ERR, null, "系统异常");

    }

    @GetMapping("topicPreview")
    public R getTeamTopicPreview(ServletRequest servletRequest) {
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
        long stuId = student.getStuId();
        List<TopicVo> innerOrOuterPublish = topicService.getInnerOrOuterPublish(username);
        return new R(Code.GET_OK, innerOrOuterPublish, "学生查看校内或校外选题概述");
    }

    @GetMapping("topicDetil/{publishId}/{topicType}")
    public R getTeamTopicDetil(@PathVariable long topicType, @PathVariable long publishId) {
        if (topicType == 1) {
            List<CampusInnerSubtaskVo> campusInnerSubtaskVos = campusInnerSubtaskVoDao.selectByPublishId(publishId);
            return new R(Code.GET_OK, campusInnerSubtaskVos, "学生查看【校内】选题详情");

        } else if (topicType == 2) {
            List<CampusOuterSubtaskVo> campusOuterSubtaskVos = campusOuterSubtaskVoDao.selectoutByPublishId(publishId);
//            List<CampusInnerSubtaskVo> campusInnerSubtaskVos = campusInnerSubtaskVoDao.selectByPublishId(publishId);
            return new R(Code.GET_OK, campusOuterSubtaskVos, "学生查看【校外】选题详情");

        } else

            return new R(Code.GET_ERR, null, "系统错误");
    }

    /**
     * author :殷凡卓
     * @param practicalId
     * @param servletRequest
     * @return
     */
    @GetMapping("topicAoDetail/{practicalId}")
    public R getTopicAoDetail(@PathVariable long practicalId, ServletRequest servletRequest) {
        List<TopicAo> topicAos = topicVoDao.selectInnerTopicAosDetail(practicalId);

        return new R(Code.GET_OK, topicAos, "教师查看学生选题情况");
    }



    @GetMapping("MytopicAoDetail/{practicalId}")
    public R getMytopicAoDetail(@PathVariable long practicalId, ServletRequest servletRequest) {

        List<TopicAo> topicAos = topicVoDao.selectOuterTopicAoDetail(practicalId);
        //    List<TopicAo> topicAos = topicVoDao.selectInnerTopicAoDetail(practicalId);

        return new R(Code.GET_OK, topicAos, "学生查看校内或校外选题概述");
    }

    @GetMapping("outerTopic/{PublishId}")
    public R getouterTopic(@PathVariable long PublishId, ServletRequest servletRequest) {

//      获取我的学号
        String username = (String) servletRequest.getAttribute("username");
        Student student = studentDao.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuPhone, username));
        long stuId = student.getStuId();
//      获取_本学期_我的member,
        MemberVo myDetil = memberVoDao.getMyDetil(stuId);
//        log.info(myDetil.toString());

        if (myDetil == null) {
            return new R(Code.TOPIC_ERR, myDetil, "您不是队长无权限，请创建队伍，或联系队长选题");
        } else if (myDetil.getMemberState() == 1 && myDetil.getTeamState() == 1) {
            //       已经队长
            return new R(Code.TOPIC_ERR, myDetil, "您已选题，不要多次选题");
        } else if (myDetil.getMemberState() == 1 && myDetil.getTeamState() == 0) {
            //      选题成功  填充Topic
            Topic topic = new Topic();
            topic.setPublishId(PublishId);
            topic.setTopicTeamId(myDetil.getTeamId());
            topic.setTopicType(2);
            topic.setTopicDate(new Date());
            topicDao.insert(topic);
            //      更改队伍表选题states

            int update = teamDao.update(null, new LambdaUpdateWrapper<Team>()
                    .eq(Team::getTeamId, myDetil.getTeamId())
                    .set(Team::getTeamState, 1));

            return new R(Code.TOPIC_OK, topic, "队长选题成功");
        } else
            return new R(Code.TOPIC_ERR, null, "系统异常");

    }


}
